/*===========================================================================+
 |   Copyright (c) 2001, 2005 Oracle Corporation, Redwood Shores, CA, USA    |
 |                         All rights reserved.                              |
 +===========================================================================+
 |  HISTORY                                                                  |
 +===========================================================================*/
package sza.oracle.apps.ap.oie.budget.webui;

import java.io.Serializable;

import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.OAViewObject;
import oracle.apps.fnd.framework.server.OADBTransaction;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OADataBoundValueViewObject;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.OAWebBeanConstants;
import oracle.apps.fnd.framework.webui.beans.OAKeyFlexBean;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageFileUploadBean;

import oracle.cabo.ui.data.DataObject;

import oracle.jbo.Row;

import sza.oracle.apps.ap.oie.budget.server.ImportFileVORowImpl;

/**
 * Controller for ...
 */
public class SzaOiePaExpenseMappingCO extends OAControllerImpl
{
  public static final String RCS_ID="$Header$";
  public static final boolean RCS_ID_RECORDED =
        VersionInfo.recordClassVersion(RCS_ID, "%packagename%");

  /**
   * Layout and page setup logic for a region.
   * @param pageContext the current OA page context
   * @param webBean the web bean corresponding to the region
   */
  public void processRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processRequest(pageContext, webBean);
    OAApplicationModule am = (OAApplicationModule)pageContext.getApplicationModule(webBean);
    am.invokeMethod("initQuery",null);
//      OAKeyFlexBean flexBean = (OAKeyFlexBean)webBean.findIndexedChildRecursive("FlexItem");
//      flexBean.setStructureCode("SZA_ACC_FLEXFIELD"); 
//      flexBean.setCCIDAttributeName("CodeCombinationId"); 
//      flexBean.mergeSegmentsWithParent(pageContext);
//      
      
      //file upload init...
      
       OAMessageFileUploadBean fileUploadBean = (OAMessageFileUploadBean)webBean.findChildRecursive("ImportFile");
             OADataBoundValueViewObject displayNameBoundValue = new OADataBoundValueViewObject(fileUploadBean,"Filename");
             fileUploadBean.setAttributeValue(DOWNLOAD_FILE_NAME,displayNameBoundValue);
          
             OAViewObject vo = (OAViewObject)am.findViewObject("ImportFileVO1");
             if (!vo.isPreparedForExecution()) {
                 vo.executeQuery();
                 ImportFileVORowImpl fileRow = (ImportFileVORowImpl)vo.createRow();
                 fileRow.setNewRowState((byte)-1);
                 vo.insertRow(fileRow);        
             }
             
             OAViewObject searchVo = (OAViewObject)am.findViewObject("SearchVO1");
              if (!searchVo.isPreparedForExecution()) {
                  searchVo.executeQuery();
                  Row fileRow = searchVo.createRow();
                  fileRow.setNewRowState((byte)-1);
                  searchVo.insertRow(fileRow);        
              }

  }

  /**
   * Procedure to handle form submissions for form elements in
   * a region.
   * @param pageContext the current OA page context
   * @param webBean the web bean corresponding to the region
   */
  public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processFormRequest(pageContext, webBean);
             OAApplicationModule am = pageContext.getApplicationModule(webBean);
             if (pageContext.getParameter("ImportBtn")!= null) {
                 ImportStrUtil csvStrUtil = new ImportStrUtil();
                 //Select the file name
                 DataObject fileUploadData = pageContext.getNamedDataObject("ImportFile");
                 String uFileName = (String)fileUploadData.selectValue(null,"UPLOAD_FILE_NAME");
                 System.out.println("uFileName == "+uFileName);
                 //check the file type
                 if (uFileName !=null && !"".equals(uFileName)) {
                     if (uFileName.length()<=4) {
                         throw new OAException("Your paper is not csv file, choose csv file uploads.",OAException.WARNING);
                     }
                     else {
                         String extension = uFileName.substring(uFileName.length()-3,uFileName.length());
                         if (extension != null && !"CSV".equals(extension.toUpperCase())) {
                             throw new OAException("Your paper is not csv file, choose csv file uploads.",OAException.WARNING);
                         }
                     }
                 }
                 else
                 {
                     throw new OAException("Please upload csv file,first.",OAException.WARNING);
                 }
               
                 OAViewObject fileVO = (OAViewObject)am.findViewObject("ImportFileVO1");
                 ImportFileVORowImpl fileRow = (ImportFileVORowImpl)fileVO.getCurrentRow();
                 //set the value of FileName
                 fileRow.setFilename(uFileName);
                 //get the fileRow context
                 String context = null;
                 int len = (int)fileRow.getFilecontext().getLength();
                 try{
                      context = new String(fileRow.getFilecontext().getBytes((long)1,len),"gb2312");
                 }
                 catch(Exception e) {
                    throw new OAException(e.getMessage()+"aaaa");
                 }
                 //get the header of upload file 
//                 String header = csvStrUtil.getHeaderLine(context);
                 //get the content of upload file
                 String[] lines = csvStrUtil.getSampleData(context);    
                 //invoke the Method "importVmi" of AM 
                 
//                 throw new OAException("test");
                 am.invokeMethod("importVmi", new Serializable[]{lines,"1"}, 
                                              new Class[]{lines.getClass(),String.class});  
                 am.invokeMethod("initQuery",null);
                 OAException message = new OAException("Done!",OAException.CONFIRMATION);
                 pageContext.putDialogMessage(message);
           
             }
             if(pageContext.getParameter("AddBtn")!=null){
               
                 pageContext.forwardImmediately("OA.jsp?page=/sza/oracle/apps/ap/oie/budget/webui/SzaOieMappingMaintainPG"
                                                , null
                                                ,OAWebBeanConstants.KEEP_MENU_CONTEXT
                                                ,null
                                                ,null
                                                ,false
                                                ,OAWebBeanConstants.ADD_BREAD_CRUMB_NO);
//                 pageContext.forwardImmediately("OA.jsp?page=/sza/oracle/apps/ap/oie/budget/webui/SzaOieMappingMaintainPG",(byte)0, null, null, true, "Y");
             }
             if(pageContext.getParameter("SearchBtn")!=null){
                 am.invokeMethod("search");
             }
          

  }

}
